<template>
    <Card>
        <JG-search :searchData="searchData" @on-input-search="askDatasearch"  />
        <JG-table
            :params="params"
            :data="data"
            :loading="loading"
            :total="total"
            :butshow="butshow"
            :columns="columns"
            @askData="askData"
            @handleDelete="handleDelete"
            @handleEdit="handleEdit"
           ></JG-table>
        <Modal v-model="modalstatus" title="修改配置">
          <Form :label-width='90' :ref='paramsname' :model='modalparams' class="setmodal">
              <FormItem label='配置的域名:'>
                  <Input style="width: 280px" v-model="modalparams.redirect_301_domain" placeholder="请输入配置的域名" />
              </FormItem>
                <FormItem label='301域名：'>
                  <p>{{modalparams.redirect_domain}}</p>
              </FormItem>
          </Form>
          <div slot="footer">
              <Button @click='canceledit(paramsname)'>取消</Button>
              <Button type='primary' :loading="subloading" @click='submitedit(paramsname)'>保存</Button>
          </div>
        </Modal>
        <Modal
            v-model="modaldel"
            title="删除提醒"
            @on-cancel="cancel" width="420">
            <p >确定要删除当前域名301跳转吗？</p>
            <div slot="footer">
                <Button type="error" :loading="modal_loading" @click="del">确定删除</Button>
                <Button @click="cancel">取消</Button>
            </div>
        </Modal>
    </Card>
</template>
<script>
    import { redirectList, redirecttoSite,deleteredirecttoSite } from '@api/jump'
    export default {
        name: '',
        data () {
            return {
                butshow: {
                  Edit:{
                      name: '-v1-site-index-update-redirect-to-site',
                      flag: '/v1/site/index/update-redirect-to-site'
                  },
                Detail:{
                      name: '-v1-site-index-delete-redirect-to-site',
                      flag: '/v1/site/index/delete-redirect-to-site'
                },
                  Delete: {
                      name: '-v1-site-index-delete-redirect-to-site',
                      flag: '/v1/site/index/delete-redirect-to-site'
                  }
                },
                modal: false,
                params: { domain: '', page: 1, pageSize: 10},
                loading: false,
                total: 0,
                searchData: {
                    left: [
                        { id: 1, key: 'Input', value: '', clearable: true, placeholder: '请输入要查询的域名', callback: 'search' }
                    ]
                },
                columns: [
                    { title: '配置的域名', key: 'redirect_domain', minWidth: 180,  },//sortable: 'custom'
                    { title: '301域名', key: 'domain', minWidth: 170, },
                    { title: '操作人', key: 'realname', minWidth: 98 },
                    { title: '添加时间', key: 'created_at_str', minWidth: 170,  },
                    { title: '更新时间', key: 'updated_at_str', minWidth: 170, },
                    { title: '操作', slot: 'jumpoption', width: 120 }
                ],
                data: [],
                modaldel: false,
                modal_loading: false,
                domain: '',
                id: null,
                modalstatus:false,
                paramsname:'paramsname',
                subloading:false,
                modalparams:{
                    redirect_domain:'',
                    redirect_301_domain:'',
                }

            }
        },
        mounted () {
            this.initial()
        },
        methods: {
            initial () {
                this.askData()
            },
            askDatas () {
                this.params.page = 1
                this.askData()
            },
            askData () {
                this.searchData.left.forEach((item) => {
                    if (item.id === 1) {
                        this.params.domain = item.value
                    }
                });
                this.loading = true
                redirectList(this.params).then(res => {
                    this.total = res.total
                    this.data = res.list
                    this.loading = false
                }).catch(() => {
                    this.loading = false;
                });
            },
            // sortchange (e) {
            //     this.log(e.key)
            //     switch (e.sort) {
            //         case 1: this.params.sort_order = 'asc'; break;
            //         case 2: this.params.sort_order = 'desc'; break;
            //         default: this.params.sort_order = ''; break;
            //     }
            //     switch (e.key) {
            //         case 'domain': this.params.sort_column = 'domain'; break;
            //         case 'created_at_str': this.params.sort_column = 'created_at'; break;
            //         case 'exec_upload_time_str': this.params.sort_column = 'exec_upload_time'; break;
            //         case 'next_exec_time_str': this.params.sort_column = 'next_exec_time'; break;
            //         case 'exec_status_str': this.params.sort_column = 'exec_status'; break;
            //         case 'type_str': this.params.sort_column = 'type'; break;
            //         case 'upload_type_str': this.params.sort_column = 'upload_type'; break;
            //         default: break;
            //     }
            //     this.askData()
            // },
            
            askDatasearch () {
                this.params.page = 1
                this.askData()
            },
            //修改
            handleEdit(row){
                this.modalparams={
                    redirect_domain:row.domain,
                    redirect_301_domain:row.redirect_domain
                }
                this.$set(this.modalparams, 'redirect_id',row.id)
                this.modalstatus=true
            },
            canceledit(){
            this.modalstatus=false
            },
            submitedit(name){
                 this.$refs[name].validate((valid) => {
                    if (valid) {
                        redirecttoSite(this.modalparams).then(res=>{
                            this.$Message.success('修改成功');
                            this.canceledit()
                             this.askData()
                        })
                        
                    }
                })
            },
            handleDelete (row) {
                this.modaldel = true
                this.domain = row.domain
                this.id = row.id
            },
            cancel () {
                this.id = null
                this.modaldel = false
                this.modal_loading = false
            },
            del () {
                this.modal_loading = true
                deleteredirecttoSite({ redirect_id: this.id }).then(() => {
                    this.$Message.info('删除成功');
                    this.askData();
                    this.cancel()
                    this.modal_loading = false
                });
            }
        }
    }
</script>
